package com.onetech.adrewards.free.dialogs;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.fedorvlasov.lazylist.ImageLoader;
import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.activities.BaseNewActivity;
import com.onetech.adrewards.free.models.CommonModel;
import com.onetech.adrewards.free.objects.AdDTO2;
import com.onetech.adrewards.free.objects.ResultDTO;
import com.onetech.adrewards.free.utils.AdrewadsUtils;
import com.zuzu.dialogs.BaseDialog;

public class AdRewardDetailDialog2 extends BaseDialog {
	private TextView newsContent=null;
	private ImageView mImageCover=null;
	private TextView mTitleAd=null;
	private TextView mChargeType=null;
	private TextView mAdPont=null;
	private TextView mCountTypeForUser=null;
	private TextView mAppWaitTime=null;
	private TextView mAppWaitTimeTitle=null;
	
	private Button mDownloadBtn=null;
	private String mInstallUrl="";
	private AdDTO2 mData=null;
	
	private Tracker mTracker;
	private Activity mActivity=null;
	
	
	
	private DoReviewBonusTask mDoReviewBonusTask=null;
	
	public AdRewardDetailDialog2(Activity context,AdDTO2 data){
		super(context, context.getString(R.string.appli_detail_title),
				TYPE_NO_BUTTON, R.layout.appli_detail_layout);
		mActivity=context;
		if(data==null){
			dismiss();
		}
		mData=data;
		
		newsContent=(TextView)findViewById(R.id.mainContent);
		mImageCover=(ImageView)findViewById(R.id.imageCover);
		mTitleAd=(TextView)findViewById(R.id.titleAd);
		mChargeType=(TextView)findViewById(R.id.chargeExp);
		mAdPont=(TextView)findViewById(R.id.adPoint);
		mCountTypeForUser=(TextView)findViewById(R.id.countTypeForUser);
		mAppWaitTime=(TextView)findViewById(R.id.appWaitTime);
		mAppWaitTimeTitle=(TextView)findViewById(R.id.appWaitTimeTitle);
		
		mDownloadBtn=(Button)findViewById(R.id.downloadBtn);
		
		try {
			mTitleAd.setText(data.name);
			
			String charge_type=AdrewadsUtils.getChargeTypeText(mActivity,data.charge_type);
			String short_desc="";
			if(data.short_desc.trim().length()<1){
				
			}else{
				short_desc+=" | " +data.short_desc;
			}
			
			mChargeType.setText(charge_type + short_desc);
			
			if(data.is_app!=null && data.is_app.equals("1")){
				mDownloadBtn.setText(mActivity.getString(R.string.button_ads_install_label));
				mDownloadBtn.setBackgroundResource(R.drawable.button_orange);
			}else{
				mDownloadBtn.setText(mActivity.getString(R.string.button_ads_get_label));
				mDownloadBtn.setBackgroundResource(R.drawable.button_blue4);
			}
			if(data.wait_time==null || data.wait_time.trim().length()<1){
				mAppWaitTimeTitle.setVisibility(View.GONE);
				mAppWaitTime.setVisibility(View.GONE);
			}else{
				mAppWaitTimeTitle.setVisibility(View.VISIBLE);
				mAppWaitTime.setVisibility(View.VISIBLE);
			}
			int sale_coin=AdrewadsUtils.convertString2Int(data.sale_coin);
			String textCoin="";		
			if(sale_coin>0){
				if(data.is_max.trim().equals("1")){
					textCoin=mActivity.getString(R.string.max_coin_text)+data.coin+"pt"+"→’"+data.sale_coin+ "pt";
				}else{
					textCoin=data.coin+"pt"+"->"+data.sale_coin +"pt";
				}
			}else{
				if(data.is_max.trim().equals("1")){
					textCoin=mActivity.getString(R.string.max_coin_text)+data.coin+"pt";
				}else{
					textCoin=data.coin+"pt";
				}
			}
			
			if(mData.id.equals("1") && "1".equals(mData.source)){
				mDownloadBtn.setText( context.getString(R.string.appli_detail_review_button));
			}
			
			mAdPont.setText(textCoin);

			newsContent.setText(Html.fromHtml(data.remark));
			mCountTypeForUser.setText(Html.fromHtml(data.remark));
			mAppWaitTime.setText(data.wait_time);
			
			mDownloadBtn.setOnClickListener(this);
			
			mInstallUrl=data.url;			
			ImageLoader.getInstance(context).DisplayImage(data.img, mImageCover);
			
		} catch (Exception e) {
		}
	}	
	@Override
	public void onClick(View v) {
		int id=v.getId();
		switch (id) {
		case R.id.downloadBtn:
			doAction();
			break;

		default:
			super.onClick(v);
			break;
		}
	}
	private void doAction(){

		try {
			AdrewadsUtils.checkUId();						
			if(mData.id.equals("1") && "1".equals(mData.source)){ //if ad house and id=1
				doReviewBonus();
				go2Market();
				return;
				
//				String reviewApp=AdrewadsUtils.getStore(getContext(), Cache.REVIEW_APP_KEY);
//				if(reviewApp!=null && reviewApp.trim().equals("Done")){
//					((HomeActivity)mActivity).isReviewApp=false;
//					go2Market();
//					return;
//				}else{
//					AdrewadsUtils.setStore(getContext(), Cache.REVIEW_APP_KEY, "Done");
//					((HomeActivity)mActivity).isReviewApp=true;
//					
//					doReviewBonus();
//					go2Market();
//					return;
//					
//				}
			}

			mInstallUrl=mData.url;
			AdrewadsUtils.go2Web(getContext(), mInstallUrl);
		} catch (Exception e) {
		}
	}
	
	private void doReviewBonus(){
		if(mDoReviewBonusTask==null || mDoReviewBonusTask.getStatus()==Status.FINISHED){
			mDoReviewBonusTask =new DoReviewBonusTask();
			mDoReviewBonusTask.execute();
		}
	}
	
	private class DoReviewBonusTask extends AsyncTask<Void, Void, ResultDTO>{
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
		}
		
		@Override
		protected ResultDTO doInBackground(Void... params) {
			try {				
				
				return CommonModel.getInstance().doReviewBonus();
				
			} catch (Exception e) {
				return null;
			}			
		}
		
		@Override
		protected void onPostExecute(ResultDTO result) {
			super.onPostExecute(result);
			try { 
				if(result!=null && result.getSuccessMessage()!=null && result.getSuccessMessage().trim().length()>0){
					((BaseNewActivity)mActivity).isReviewApp=true;
					((BaseNewActivity)mActivity).reviewSuccessMessage = result.getSuccessMessage();								
				}
				
			} catch (Exception e) {
				
			}
			finally{
				
			}
		}
	}
		
	private void go2Market()
	{
		Uri uri = Uri.parse("market://details?id=" + mActivity.getPackageName());
		Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
		try {
			mActivity.startActivity(goToMarket);
		} catch (ActivityNotFoundException e) {
			mActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + mActivity.getPackageName())));
		}
	}
	
	
	@Override
    public void onStart() {
        super.onStart();
        try {
            mTracker.set(Fields.SCREEN_NAME, mActivity.getString(R.string.ExchangeFragment));            
            mTracker.send(MapBuilder.createAppView().build());            
		} catch (Exception e) {
		}
    }
}
